﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inicio.Forms.Obras;
using System.Data.SqlServerCe;
using Inicio.Entities;

namespace Inicio
{
    public partial class ucSearchField : UserControl
    {
        public object Value
        {
            get
            {
                if (pnlValue.Controls.Count == 1)
                {
                    Control ctl = pnlValue.Controls[0];
                    if (ctl is TextBox)
                        return ((TextBox)ctl).Text.Trim();
                    if (ctl is ComboBox)
                        return ((ComboBox)ctl).SelectedValue;
                    if (ctl is NumericUpDown)
                        return ((NumericUpDown)ctl).Value;
                }
                return null;
            }
        }

        public SearchField Field 
        {
            get
            {
                if (cbField.SelectedValue is SearchField && cbField.SelectedValue != null)
                    return (SearchField)cbField.SelectedValue;
                else
                    return null;
            }
        }

        SearchObra parent;
        List<SearchField> fields;

        //public ucSearchField(SearchObra parent)
        //{
        //    this.parent = parent;
        //    InitializeComponent();
        //    cbOperator.SelectedIndex = 0;
        //    cbField.DisplayMember = "table_name";

        //    cbField.DataSource = CargoCbTable();
        //}

        public ucSearchField(SearchObra parent, List<SearchField> fields)
        {
            this.fields = fields;
            this.parent = parent;
            
            InitializeComponent();
            cbOperator.SelectedIndex = -1;

            pnlValue.Font = new Font(pnlValue.Font.FontFamily, 12);

            cbField.DisplayMember = "Label";
            cbField.DataSource = fields;
            cbField.SelectedIndex = -1;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            parent.NewControl();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            parent.RemoveControl(this);
        }

        private DataTable CargoCbTable()
        {
            string con = Core.Connection.conec;
            try
            {
                SqlCeConnection cn = new SqlCeConnection(con);

                cn.Open();
                SqlCeCommand cmd = new SqlCeCommand("select table_name from information_schema.tables where TABLE_TYPE = 'TABLE'", cn);

                SqlCeDataAdapter da = new SqlCeDataAdapter("select table_name from information_schema.tables where TABLE_TYPE = 'TABLE'", cn);
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;


            }
            catch (Exception)
            {

                throw;
            }

        }

        private void cbField_SelectedIndexChanged(object sender, EventArgs e)
        {
            SearchField field;
            Control ctl;
            cbOperator.Items.Clear();

            pnlValue.Controls.Clear();
            if (cbField.SelectedValue is SearchField && cbField.SelectedValue != null)
            {
                field = (SearchField)cbField.SelectedValue;
                switch (field.Control)
                {
                    case "TextBox":
                        ctl = new TextBox();
                        pnlValue.Controls.Add(ctl);
                        cbOperator.Items.Add("=");
                        break;
                    case "ComboBox":
                        ctl = new ComboBox();
                        ((ComboBox)ctl).DisplayMember = field.DisplayField;
                        ((ComboBox)ctl).ValueMember = field.ValueField;
                        ((ComboBox)ctl).DataSource =  GetDataSource(field);
                        pnlValue.Controls.Add(ctl);
                        ((ComboBox)ctl).DropDownStyle = ComboBoxStyle.DropDownList;
                        cbOperator.Items.Add("=");
                        break;
                    case "NumericBox":
                        ctl = new NumericUpDown();
                        ((NumericUpDown)ctl).Minimum = 0;
                        ((NumericUpDown)ctl).Maximum = 10000;
                        ((NumericUpDown)ctl).DecimalPlaces = 0;
                        pnlValue.Controls.Add(ctl);
                        cbOperator.Items.Add("=");
                        cbOperator.Items.Add(">");
                        cbOperator.Items.Add("<");
                        break;
                    default:
                        ctl = new TextBox();
                        pnlValue.Controls.Add(ctl);
                        break;
                }
                cbOperator.SelectedIndex = 0;
            }
        }

        private DataTable GetDataSource(SearchField field)
        {
            DataTable dt = new DataTable();
            string qry = string.Format("SELECT * FROM {0}", field.Table);

            using (SqlCeConnection cn = new SqlCeConnection(Core.Connection.conec))
            {
                using (SqlCeDataAdapter da = new SqlCeDataAdapter(qry, cn))
                {
                    da.Fill(dt);
                }
            }
            return dt;
        }

        private void ucSearchField_Load(object sender, EventArgs e)
        {

        }
    }
}
